import { DataProxy } from "../../frame/A-LIB/lib.b.data";
import { AudioManager } from "../../frame/component/audio-manager";
import { APP_NAME_GAME } from "../../frame/frame.config";
import get_proxy_status from "../data/game.data.status";
import get_proxy_trigger from "../data/game.data.trigger";
import get_proxy_user from "../data/game.data.user";

export default class GGameDC {
	private static _user_: ReturnType<typeof get_proxy_user>;
	private static _trigger_: ReturnType<typeof get_proxy_trigger>;
	private static _status_: ReturnType<typeof get_proxy_status>;

	public static get User(){
		return GGameDC._user_;
	}
	public static get Trigger(){
		return GGameDC._trigger_;
	}
	public static get Status(){
		return GGameDC._status_;
	}

	private static __guide_inited__: mtec.NudityPromise<'end'>;
	public static get guideInited(){
		return GGameDC.__guide_inited__.promise;
	}

	public static init(){
		GGameDC._user_ = get_proxy_user('Ggame_user', APP_NAME_GAME);
		GGameDC._trigger_ = get_proxy_trigger('Ggame_trigger');
		GGameDC._status_ = get_proxy_status('Ggame_status');

		GGameDC.__guide_inited__ = new mtec.NudityPromise<'end'>();

		GGameDC.listening();
	}

	public static listening(){

		DataProxy.follow(GGameDC.User, 'bgm', (_, pick)=>{
			AudioManager.mute(!pick);
		}, false);

		if(GGameDC.Status.guideInited){
			GGameDC.__guide_inited__.resolve('end');
		}else{
			let reg = DataProxy.follow(GGameDC.Status, 'guideInited', ()=>{
				DataProxy.cancel(reg);
				GGameDC.__guide_inited__.resolve('end');
			}, false);
		}
	}

	public static checkGuideID(id: number){
		let reg = new RegExp('(?<!\\d)'+id+'(?!\\d)', 'gm');
		return reg.test(GGameDC.User.guide_record);
	}

	public static DoneGuideID(id: number){
		if(GGameDC.checkGuideID(id)){
			return void 0;
		}

		if(GGameDC.User.guide_record==''){
			GGameDC.User.guide_record += id;
		}else{
			GGameDC.User.guide_record += ':'+id;
		}
	}

	public static CleanGuideID(...list: number[]){
		if(list.length>0){
			let guid_done = GGameDC.User.guide_record;
			list.forEach(id=>{
				let reg = new RegExp('(?<!\\d)'+id+'(?!\\d)', 'gm');
				guid_done = guid_done.replace(reg, '');
			});
			GGameDC.User.guide_record = guid_done.replaceAll(/(?<!\d):/gm, '').replaceAll(/:(?!\d)/gm, '');
		}
	}
}